package com.woniu.car.controller;

import com.woniu.bean.R;
import com.woniu.car.bean.Login;
import com.woniu.car.service.ILoginService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author kk
 * @since 2022-08-15
 */
@RestController
@RequestMapping("/login")
@CrossOrigin
public class LoginController {
    @Autowired
    private ILoginService loginService;

    @PostMapping
    public R login(Login login){
        R r =null;
        Subject subject = SecurityUtils.getSubject();
        //创建token 拿到账户密码
        UsernamePasswordToken token = new UsernamePasswordToken(login.getUsername(),login.getPassword());
        //如果未登录
        if(!subject.isAuthenticated()){
            try{
                //登陆
            subject.login(token);
            r=R.OK();
            r.setMsg("成功");
            return  r;
            }catch (AuthenticationException authenticationException){
                System.out.println("认证失败");

            }
        }
        r=R.ERROR();
        r.setMsg("失败");
        return r;
    }
}
